import { ApiProperty } from '@nestjs/swagger'
import { BaseEntity, Column, CreateDateColumn, Entity, Index, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'

import { UserEntity } from '~/modules/user/user.entity'

@Entity({ name: 'health_user_sos' })
@Index(['startAt', 'endAt'], { unique: true })
export class UserSosEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ name: 'user_id', nullable: false, comment: '用户ID' })
  @ApiProperty({ description: '用户ID' })
  userId: number

  @Column({ comment: '设备ID' })
  @ApiProperty({ description: '设备ID' })
  uid: string

  @Column({ comment: '状态', default: 0 })
  @ApiProperty({ description: '状态' })
  status: number

  @Column({ comment: '次数', default: 1 })
  @ApiProperty({ description: '次数' })
  times: number

  @CreateDateColumn({ name: 'start_at' })
  @ApiProperty({ description: '开始时间' })
  startAt: Date

  @Column({ name: 'end_at', nullable: true })
  @ApiProperty({ description: '结束时间' })
  endAt: Date

  @ManyToOne(() => UserEntity, user => user.id, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'user_id', referencedColumnName: 'id' })
  user: UserEntity
}
